jQuery.fn.countdown = function(userOptions)
{
  var options = {
    stepTime: 60,
    format: "dd:hh:mm:ss",
    endTime: "November 17, 2011 11:13:00",
    timerEnd: function(){},
    image: "counter01.png"
  };
  var digits = [], interval;
  
  var createDigits = function(id) 
  {
  	$this = $('#' + id);
  	dateNow = new Date(); 
  	dateFuture = new Date(options.endTime);
  	amount = dateFuture.getTime() - dateNow.getTime()+5;   
  	delete dateNow;  

  	/* time is already past */
    if(amount < 0){
    	clearInterval(interval);
      options.timerEnd();
      return;
    }else
    {
			days=0;hours=0;mins=0;secs=0;out="";
			amount = Math.floor(amount/1000); /* kill the milliseconds */

			days=Math.floor(amount/86400); /* days */
			amount=amount%86400;

			hours=Math.floor(amount/3600); /* hours */
			amount=amount%3600;

			mins=Math.floor(amount/60); /* minutes */
			amount=amount%60;

			secs=Math.floor(amount);


			out=
				"<div id='countdays'><span></span>" + days +"<div class='countdown_text'>天</div></div>" + 
				"<div id='counthours'><span></span>" + hours +"<div class='countdown_text'>時</div></div>" + 
				"<div id='countmins'><span></span>" + mins +"<div class='countdown_text'>分</div></div>" + 
				"<div id='countsecs'><span></span>" + secs +"<div class='countdown_text'>秒</div></div>" ;
			$this.html(out);			
    }  	  
  };

  $.extend(options, userOptions);
  interval = setInterval(createDigits($(this).attr('id')) , 1000);
};



